package com.lxinet.jeesns.modules.wechat.utils;

import com.blade.kit.json.JSONArray;
import com.blade.kit.json.JSONKit;
import com.blade.kit.json.JSONObject;
import com.lxinet.jeesns.modules.wechat.entity.WechatMeta;
import com.lxinet.jeesns.utils.JedisUtil;
import redis.clients.jedis.Jedis;

/**
 * Created by Administrator on 2017/5/10.
 */
public class HanderName {
    public static String getNickNameByName(WechatMeta wechatMeta, String name) {
        Jedis jedis = JedisUtil.getInstance().getJedis();
        String nameName = "这个人物名字未知";
        String curUserName=wechatMeta.getUser().get("UserName").toString();
        curUserName=curUserName.substring(1,curUserName.length()-1);
        if(curUserName.equals(name)){
            String curNickName=wechatMeta.getUser().get("NickName").toString();
            return curNickName.substring(1,curNickName.length()-1);
        }
        String s = jedis.get(wechatMeta.getWxuin().toString());
        JSONObject webcontact = JSONKit.parseObject(jedis.get(wechatMeta.getWxuin().toString()));
        if(webcontact!=null&&webcontact.get("groupChatList")!=null) {
            JSONArray groupChatList=(JSONArray)webcontact.get("groupChatList");
            for (int i = 0, len = groupChatList.size(); i < len; i++) {
                JSONObject groupChat =groupChatList.get(i).asJSONObject();
                if (groupChat.getString("UserName").equals(name)) {
                    nameName=groupChat.getString("NickName");
                    return nameName;
                }
            }
        }
        if(webcontact!=null&&webcontact.get("contactList")!=null) {
            JSONArray contactList=(JSONArray)webcontact.get("contactList");
            for (int i = 0, len = contactList.size(); i < len; i++) {
                JSONObject contact =contactList.get(i).asJSONObject();
                if (contact.getString("UserName").equals(name)) {
                    nameName=contact.getString("NickName");
                    return nameName;
                }
            }
        }
        if(webcontact!=null&&webcontact.get("memberList")!=null) {
            JSONArray memberList=(JSONArray)webcontact.get("memberList");
            for (int i = 0, len = memberList.size(); i < len; i++) {
                JSONObject member =memberList.get(i).asJSONObject();
                if (member.getString("UserName").equals(name)) {
                    nameName=member.getString("NickName");
                    return nameName;
                }
            }
        }
        return nameName;
    }
}
